package com.runshu.newsmartark.base;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;


import com.runshu.newsmartark.event.BindEventBus;
import com.runshu.newsmartark.event.EventBusUtils;

import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * <pre>
 *     time   : 2019/03/01
 *     desc   : Fragment基类
 *     version: 1.0   初始化
 *     params:
 *  <pre>
 */
public abstract class BaseLazyFragmentPlus extends Fragment {
    protected final String TAG = this.getClass().getSimpleName();

    protected Context context;
    private Unbinder mBind;
    private boolean isPrepared;
    private boolean isFirst = false;
    private boolean isVisible;
    protected View mRootView;
    protected View mLayoutNetError;
    protected View mLayoutContent;


    @LayoutRes
    protected abstract int getLayoutId ();

    protected abstract void initData ();


    @Override
    public void onActivityCreated (@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        isPrepared = true;
    }

    @Override
    public void onAttach (@NonNull Context context) {
        super.onAttach(context);

    }

    @Nullable
    @Override
    public View onCreateView (@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                              @Nullable Bundle savedInstanceState) {
        if (mRootView == null) {
            mRootView = inflater.inflate(getLayoutId(), container, false);
        }
        mBind = ButterKnife.bind(this, mRootView);
        // 若使用BindEventBus注解，则绑定EventBus
        if (this.getClass().isAnnotationPresent(BindEventBus.class)) {
            EventBusUtils.register(this);
        }
        return mRootView;
    }

    @Override
    public void onViewCreated (@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        context = getActivity();
        isFirst = true;
        initView(view);
    }

    protected abstract void initView (View layoutInflateView);

    @Override
    public void setUserVisibleHint (boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint() && isFirst && isPrepared) {
            isVisible = true;
            isFirst = false;
            lazyLoad();
        } else {
            isVisible = false;
        }
    }

    private void lazyLoad () {
        if (isVisible) {
            initData();
            bindListener();
        }
    }

    protected abstract void bindListener ();

    @Override
    public void onResume () {
        super.onResume();
        setUserVisibleHint(getUserVisibleHint());
    }

    @Override
    public void onDestroy () {
        super.onDestroy();
        if (mBind != null) {
            mBind.unbind();
            mBind = null;
        }
        // 若使用BindEventBus注解，则解绑定EventBus
        if (this.getClass().isAnnotationPresent(BindEventBus.class)) {
            EventBusUtils.unregister(this);
        }
        isPrepared = false;
        context = null;
        mRootView = null;
        mLayoutNetError = null;
        mLayoutContent = null;
    }
}
